package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeQuery;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;

import java.util.List;

/**
 * @author 丧彪
 */
@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    
    @RequestMapping("")
    public String index(){
        return "employee/list";
    }
    
    @RequiresPermissions("employee:list")
    @PermissionName("员工列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(EmployeeQuery qo){
        //获取当前用户
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        //HR才有查看所有员工数据的权限
        if (!SecurityUtils.getSubject().hasRole("HR_MGR")) {
            qo.setCurrentUserId(employee.getId());
        }
        return employeeService.query(qo);
    }

    @RequestMapping("password")
    public String password(){
        return "changepassword/password";
    }

    @RequestMapping("save")
    @ResponseBody
    @RequiresPermissions("employee:save")
    @PermissionName("员工新增")
    public JsonResult save(Employee entity){
        JsonResult result = new JsonResult();
        try {
            employeeService.save(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("保存操作失败" + e.getMessage());
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("employee:update")
    @PermissionName("员工更新")
    public JsonResult update(Employee entity){
        JsonResult result = new JsonResult();
        try {
            employeeService.update(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("编辑操作失败" + e.getMessage());
        }
        return result;
    }
    
    //唯一账号校验 
    @RequestMapping("validateUsername")
    @ResponseBody
    public boolean validateUsername(String username, Long id){
    	Employee employee = employeeService.validateUsername(username);
    	if(id != null) {
    		//编辑时不需要校验是否重名
    		return true;
    	}
        return employee == null;
    }
    
    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions("employee:changeState")
    @PermissionName("员工状态改变")
    public JsonResult changeState(Long id){
        JsonResult result = new JsonResult();
        try {
           employeeService.changeState(id);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("操作失败" + e.getMessage());
        }
        return result;
    }

    @RequestMapping("changePassword")
    public String  changePassword(String password) {
        employeeService.changePassword(password);
        SecurityUtils.getSubject().logout();
        return "redirect:/logout.jsp";
    }

    //查所有员工数据,下拉框使用
    @RequiresPermissions("employee:all")
    @PermissionName("所有员工")
    @RequestMapping("allEmps")
    @ResponseBody
    public List<Employee> allEmps(){
        return employeeService.list();
    }

}
